package com.init.engineering.service.impl;

import com.init.engineering.dao.ItemDao;
import com.init.engineering.pages.PageResult;
import com.init.engineering.pages.QueryPageBean;
import com.init.engineering.pojo.Item;
import com.init.engineering.pojo.Subitem;
import com.init.engineering.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ItemServiceImpl implements ItemService {
    @Autowired(required = false)
    private ItemDao itemDao;

    @Override
    public List<Item> findItems() {
        List<Item> items = itemDao.findItems();
        return items;
    }

    @Override
    public PageResult findItemsByPage(QueryPageBean queryPageBean) {
        Integer begin = (queryPageBean.getCurrentPage() - 1) * queryPageBean.getPageSize();
        queryPageBean.setCurrentPage(begin);
        //查询当前页的数据集合
        List<Item> items = itemDao.finditemsByPage(queryPageBean);
        //获取分页查询的条件
        String queryString = queryPageBean.getQueryString();
        //获取分页查询按条件查询的总条数
        long totalCount = itemDao.selectTotalCount(queryString);

        //将数据封装到PageResult内
        PageResult pageResult = new PageResult();
        pageResult.setTotal(totalCount);
        pageResult.setRows(items);
        return pageResult;
    }

    @Override
    public int saveItem(Item item) {
        int rows = itemDao.saveItem(item);
        return rows;
    }

    @Override
    public int deleteItem(String iid) {
        int rows = itemDao.updateItemStatus(iid);
        return rows;
    }

    @Override
    public Item findItemByIid(String iid) {
        Item item  = itemDao.findItemByIid(iid);
        return item;
    }

    @Override
    public int updateItem(Item item) {
        int rows = itemDao.updateItem(item);
        return rows;
    }

    @Override
    public List<Item> findItemsBySuid(String suid) {
        List<Item> items = itemDao.findSubitemBySuid(suid);
        return items;
    }
}
